<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BgcmsTuLCustomer extends BgcmsProcess {
	var $db;
 	var $smarty;	
	public function __construct($db, $smarty) {
		$this->db = $db;
		$this->smarty = $smarty;
	}
		
	public function __destruct() {
		//
	}
	
	public function show_customer_list(){
		$customers = array();
		$countcustomer = BgcmsTuLCustomerBean::count_all_customer($this->db);
		$customers['data'] = BgcmsTuLCustomerBean::get_all_customer_paging($this->db,ADMINPAGING,"list");
		$result = BgcmsPaging::do_paging($countcustomer,$customers, ADMINPAGING, "?module=BgcmsTuLCustomer&sub1=CustomerList","list");
		
		$this->smarty->assign("CUSTOMERS", $result);
		$this->smarty->display('BgcmsTuLCustomer/index.tpl');
	}
	
  public function process(){
		$sub1 = "CustomerList";
		if(isset($_GET['sub1'])) {
			$sub1 = $_GET['sub1'];
		}
		if($sub1 == "") $sub1 = "CustomerList";
		
		if($sub1 == "CustomerList" || $sub1 == "editcustomer") {
			$this->process_customer($sub1);
		} elseif ($sub1 == "CustomerBilling") {
			$this->process_customer_billing($sub1);
		}
		$this->show_customer_list();
	}
	
	public function process_customer($sub1) {
		if(isset($_GET['mess'])) {
			$msg = "";
			switch($_GET['mess']) {
				case 1:
					$msg = _("Customer updated.");
					break;
				case 2:
					$msg = _("Customer not updated. Maybe you have not change anything.");
					break;
				case 3:
					$msg = _("Customer has been added");
					break;		
			}	
			$this->smarty->assign("resp_message", $msg);
		}
		if(isset($_POST['act'])) {
			$act = $_POST['act'];
			if($act == "addcustomer") {
				$this->create_customer();
			} elseif ($act == "editcustomer") {
				$this->edit_customer();
			} elseif($act == "delcustomer") {
				$this->delete_customer();
			}
		} else {
			if($sub1 == "editcustomer") {
				$id = @$_GET['id'];
				$customer = BgcmsTuLCustomerBean::get_customer_by_id($this->db, $id);
				$this->smarty->assign("THECUSTOMER", $customer);
			}
		}
	}
	
	public function create_customer() {
		$msg = BgcmsTuLCustomer::validate_customer($this->db);
		$arr = array("code" => -1, "message" => _("Could not add Customer."));
		if($msg == "") {
			$id = BgcmsTuLCustomerBean::save_customer($this->db, $_POST);
			if($id >=0 ) {
				$arr = array("code" => 0, "message" => _("New Customer has been added."), "customer_id" => $id);
				$arr['linkactive'] = "?module=BgcmsTuLCustomer&sub1=CustomerList&mess=3";
			}
		} else {
			$arr = array("code" => -1, "message" => $msg);
		}
		echo array2json($arr);
				
	}
	
	public function edit_customer() {
		if(isset( $_POST['customer_id'])) {
			$msgId = 2;
			$msg = $this->validate_customer($this->db);
			if($msg == "") {
				$res = BgcmsTuLCustomerBean::update_customer($this->db, $_POST);
				$msgId = 1;
				$arr = array('ID' => 0,'msg' => 1);
				$arr['linkactive'] = "?module=BgcmsTuLCustomer&sub1=CustomerList&mess=1";	
			} else {
				$arr = array('ID' => -1, 'message' => $msg);
			}
			echo array2json($arr);
		}
	}
	
	public function delete_customer() {
		$arr = array('code' => 0, 'message' => "");
		$id = @$_POST['id'];
		$res = BgcmsTuLCustomerBean::delete_customer_by_id($this->db, $id);
		if($res == true) {
			$arr['code'] = 0;
			$arr['message'] = sprintf(_("Customer <u>%s</u> has been deleted."), $id);
		} else {
			$arr['code'] = -1;
			$arr['message'] = sprintf(_("Could not delete Customer <u>%s</u>."), $id);
		}
		echo array2json($arr);
	}
	
    public static function validate_customer($db) {
      $msg = "";
    	if($_POST['customer_firstname'] == "") {
			 $msg = "Please type the Firtname.";
		  }
		  if($_POST['customer_lastname'] == "") {
			 $msg = "Please type the Lastname.";
		  }
      if(!isset($_POST['customer_email'])) {
			   if(BgcmsTuLCustomerBean::existed_customer_by_email($db, $_POST['customer_email'])) {
            if($msg != "") $msg .= "<br />";
              $msg .= "This Email is already registered.";
			   }
		  }
      if($_POST['customer_email'] == "") {
			   if($msg != "") $msg .= "<br />";
			     $msg .= "Please type an e-mail address.";
		  }
		  if($_POST['customer_email'] != "") {
			   $flag = BgcmsTuLCustomer::check_email_address($_POST['customer_email']);
			   if($flag == false) {
				    if($msg != "") $msg .= "<br />";
				      $msg .= "Invalid email address.";
			   }
		  }
  		if($_POST['customer_password'] == "") {
  			if($msg != "") $msg .= "<br />";
  			$msg .= "Password fields can not be empty.";
  		}
  		if($_POST['customer_password'] != "" && strlen($_POST['customer_password']) < 6) {
  			if($msg != "") $msg .= "<br />";
  			$msg .= "Your password must contain at least 6 characters.";
  		}
  		if($_POST['customer_password'] != $_POST['customer_password2']) {
  			if($msg != "") $msg .= "<br />";
  			$msg .= "Please type the same password in the two password fields.";
  		}
  		
		  return $msg;
    }
    //@TODO in LIBS
    function check_email_address($email) {
    	// First, we check that there's one @ symbol, and that the lengths are right
    	if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) 
    	{
    	// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
    	return false;
    	}
    	// Split it into sections to make life easier
    	$email_array = explode("@", $email);
    	$local_array = explode(".", $email_array[0]);
   		for ($i = 0; $i < sizeof($local_array); $i++) 
   		{
   			if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) 
   			{
   			return false;
   			}
   		}
   		if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1]))  // Check if domain is IP. If not, it should be valid domain name
   		{
   			$domain_array = explode(".", $email_array[1]);
   			if (sizeof($domain_array) < 2) 
   			{
   			return false; // Not enough parts to domain
   			}
   			for ($i = 0; $i < sizeof($domain_array); $i++) 
   			{
   				if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) 
   				{
   				return false;
   				}
   			}
   		}
   		return true;
	}
    
}
?>